﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Galoap
{
    /// <summary>
    ///  A triple <subject , predicate , object>
    /// </summary>
    /// 
    [Serializable()]
    public class Ontology : ISerializable
    {
        /// <summary>
        /// the subject of the ontology
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// the predicate of the ontology
        /// </summary>
        public string Predicate { get; set; } 

        /// <summary>
        /// the object of the ontology
        /// </summary>
        public string Object { get; set; }

        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="_subject">the subject</param>
        /// <param name="_predicate">the predicate</param>
        /// <param name="_object">the object</param>
        public Ontology(string _subject, string _predicate, string _object)
        {
            this.Predicate = _predicate;
            this.Subject = _subject;
            this.Object = _object;
        }


        /// <summary>
        /// override toString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "[\"" + Subject + "\",\"" + Object + "\"]";
        }


        /// <summary>
        /// Convert a string into an Ontology, erase exception if elements contains ","
        /// </summary>
        /// <param name="s">the string must b in the format <subject,predicate,object> and must not contain ","</param>
        /// <returns>an ontology</returns>
        public static Ontology FromString(string s)
        {
            string[] str = System.Text.RegularExpressions.Regex.Split(s, ",");
            if (str.Length == 3)
                return new Ontology(str[0].Substring(1, str[0].Length - 1), str[1], str[2].Substring(0, str[2].Length - 1));
            else
                throw new InvalidOperationException();
        }

        /// <summary>
        /// For serialization
        /// </summary>
        /// <param name="info">the serialization info</param>
        /// <param name="ctxt">the streaming context</param>
        public Ontology(SerializationInfo info, StreamingContext ctxt)
        {
            Subject = (string) info.GetValue("Subject", typeof(string));
            Predicate = (string)info.GetValue("Predicate", typeof(string));
            Object = (string)info.GetValue("Object", typeof(string));
        }

        /// <summary>
        /// For serialization
        /// </summary>
        /// <param name="info">the serialization info</param>
        /// <param name="ctxt">the streaming context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Subject", Subject);
            info.AddValue("Predicate", Predicate);
            info.AddValue("Object", Object);

        }

    }
}
